/*
 * Copyright (C) 2012-2013  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.launch.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.net.URL;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
import javax.swing.border.BevelBorder;

/**
 * SwingSplashScreen : The Swing-build splash screen.
 * 
 * @author Romain DUBOIS
 */
public class SwingSplashScreen implements ISplashScreen {

    /** The splash screen window */
    private final JWindow mWindow;
    /** The progress bar */
    private final JProgressBar mProgressBar;
    /** The background component */
    private final JLabel mBackground;

    /**
     * Constructor
     */
    public SwingSplashScreen() {
        super();

        mBackground = new JLabel();
        mBackground.setLayout(new BorderLayout());
        mBackground.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));

        mWindow = new JWindow();
        mWindow.setBackground(Color.DARK_GRAY);
        mWindow.setContentPane(mBackground);

        mProgressBar = new JProgressBar();
        mProgressBar.setStringPainted(true);
        mProgressBar.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLoweredBevelBorder(),
                BorderFactory.createEmptyBorder(-1, -1, -1, -1)));
        mProgressBar.setMaximum(100);

        final JPanel lWrapper = new JPanel(new BorderLayout());
        lWrapper.setOpaque(false);
        lWrapper.add(mProgressBar);
        lWrapper.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        mBackground.add(lWrapper, BorderLayout.SOUTH);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.launch.swing.ISplashScreen#setProgress(int, java.lang.String)
     */
    @Override
    public void setProgress(final int pValue, final String pMessage) {
        if (!mWindow.isVisible()) {
            mWindow.pack();
            mWindow.setLocationRelativeTo(null);
            mWindow.setVisible(true);
        }

        mProgressBar.setValue(Math.max(pValue, mProgressBar.getValue()));
        mProgressBar.setString(pMessage);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.launch.swing.ISplashScreen#close()
     */
    @Override
    public void close() {
        mWindow.dispose();
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.launch.swing.ISplashScreen#setEmptyProgressColor(java.awt.Color)
     */
    @Override
    public void setEmptyProgressColor(final Color pColor) {
        mProgressBar.setBackground(pColor);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.launch.swing.ISplashScreen#setFilledProgressColor(java.awt.Color)
     */
    @Override
    public void setFilledProgressColor(final Color pColor) {
        mProgressBar.setForeground(pColor);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.launch.swing.ISplashScreen#setEmptyTextColor(java.awt.Color)
     */
    @Override
    public void setEmptyTextColor(@SuppressWarnings("unused") final Color pColor) {
        // Can not be configured
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.launch.swing.ISplashScreen#setFilledTextColor(java.awt.Color)
     */
    @Override
    public void setFilledTextColor(@SuppressWarnings("unused") final Color pColor) {
        // Can not be configured
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.launch.swing.ISplashScreen#setBackgroundImage(java.net.URL)
     */
    @Override
    public void setBackgroundImage(final URL pImageUrl) {
        mBackground.setIcon(new ImageIcon(pImageUrl));
    }
}
